﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using Bzure.Security.Permissions;
using Bzure.Common.Core;
using Bzure.Common.Patterns;
using Bzure.Common.Caching;

namespace Bzure.Security.Principal
{
    // [Serializable] 此处不能加序列化标志，否则运行时出错，不知道为什么
    public class BzurePrincipal : IPrincipal
    {
        public BzurePrincipal(BzureUserModel user)
        {
            _user = user;
            this.TicketData = "";
        }
        BzureUserModel _user;
        public IIdentity Identity
        {
            get
            {
                return _user;
            }
        }
        public int RoleValue
        {
            get
            {
                return _user.Entity.RoleValue;
            }
        }
        [Obsolete("已经被禁用,请使用RoleValue属性", true)]
        public bool IsInRole(string role)
        {
            throw new NotImplementedException();
        }
        public string TicketData
        {
            get;
            set;
        }
        public BzureUserModel ToUserModel()
        {
            return _user;
        }
    }
    public static class userPermissionExtensions
    {
        public static UserPermission GetPermission(this BzurePrincipal Principal)
        {
            return Actor.Me.GetCached("_bzure.security.userpermission", () =>
            {
                BzureUserModel um = Principal.Identity as BzureUserModel;
                return new UserPermission(um.Entity.UserID.ToString(), um.Entity.UserName);
            }, TimeSpan.FromHours(2));
        }
    }
}
